<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item> <i class="el-icon-lx-cascades"></i> 工资管理 </el-breadcrumb-item>
                <el-breadcrumb-item> 参数查询 </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-button type="primary" icon="el-icon-delete" class="handle-del mr10" @click="delAllSelection">批量删除</el-button>
                <el-select v-model="query.did" placeholder="部门" class="handle-select mr10">
                    <el-option label="全部部门" value=""></el-option>
                    <el-option v-for="(item,index) in depts" :key="index" :label="item.name" :value="item.id"></el-option>
                </el-select>
                <el-input v-model="query.name" placeholder="职位名称" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
                <el-button type="warning" icon="el-icon-plus" @click="dialogFormVisible = true">添加参数</el-button>
            </div>
            <el-table
                :data="tableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column type="index" label="ID" width="55" align="center"></el-table-column>
                <el-table-column prop="pid" label="职位名称" :formatter="formatterPName"></el-table-column>
                <el-table-column prop="basicSalary" label="基础工资/月"></el-table-column>
                <el-table-column prop="fullAttention" label="全勤奖励/月"></el-table-column>
                <el-table-column prop="positionSalary" label="职位补贴/月"></el-table-column>
                <el-table-column prop="subsidy" label="其他补贴/月"></el-table-column>
                <el-table-column prop="overtimeSalary" label="加班补贴/时"></el-table-column>
                <el-table-column label="操作" align="center" width="350px">
                    <template slot-scope="scope">
                        <el-button type="success" size="mini" icon="el-icon-view" @click="view(scope.row.id)">详情</el-button>
                        <el-button type="primary" size="mini" icon="el-icon-edit" @click="edit(scope.row.id)">编辑</el-button>
                        <el-button type="danger" v-if="scope.row.id!='1'" size="mini" icon="el-icon-close" @click="del(scope.row.id)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="query.pageNum"
                    :page-size="query.pageSize"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
            <el-dialog title="添加参数" :visible.sync="dialogFormVisible" width="500px">
                <el-form :model="form" :rules="rules" ref="form">
                    <el-form-item>
                        <el-alert
                            title="消息提示的文案"
                            type="info"
                            description="平时加班,时薪×时间数×150%，未满一小时者不算。
                                迟到、早退时扣“时资×时间数×150%”的工资。除休假、请假外，其他未事先向公司请假而缺勤者视为
                                旷职。旷职者加扣“日薪×旷职日数×100％”的工资。事假的扣薪为日薪扣除."
                            show-icon
                        >
                        </el-alert>
                    </el-form-item>

                    <el-form-item label="职位" prop="pid" :label-width="labelWidth">
                        <el-select v-model="form.pid" placeholder="请选择职位">
                            <el-option v-for="(item, index) in paramPositions" :key="index" :label="item.name" :value="item.id"></el-option>
                        </el-select>
                    </el-form-item>

                    <el-form-item label="基础工资" prop="basicSalary" :label-width="labelWidth">
                        <el-input placeholder="基础工资/每月" v-model.number="form.basicSalary" @change="setParam"></el-input>
                    </el-form-item>

                    <el-form-item label="全勤补贴" prop="fullAttention" :label-width="labelWidth">
                        <el-input placeholder="全勤补贴/每月" v-model.number="form.fullAttention"></el-input>
                    </el-form-item>

                    <el-form-item label="岗位补贴" prop="positionSalary" :label-width="labelWidth">
                        <el-input placeholder="岗位补贴/每月" v-model.number="form.positionSalary"></el-input>
                    </el-form-item>

                    <el-form-item label="加班/小时" prop="overtimeSalary" :label-width="labelWidth">
                        <el-input placeholder="加班工资/每时" v-model.number="form.overtimeSalary"></el-input>
                    </el-form-item>

                    <el-form-item label="其他补贴" prop="subsidy" :label-width="labelWidth">
                        <el-input placeholder="其他补贴/每月" v-model.number="form.subsidy"></el-input>
                    </el-form-item>

                    <el-form-item label="请假/小时" prop="leaveDeduction" :label-width="labelWidth">
                        <el-input placeholder="请假扣除/每时" v-model.number="form.leaveDeduction"></el-input>
                    </el-form-item>

                    <el-form-item label="迟到/小时" prop="lateDeduction" :label-width="labelWidth">
                        <el-input placeholder="迟到扣除/每时" v-model.number="form.lateDeduction"></el-input>
                    </el-form-item>

                    <el-form-item label="其他扣/天" prop="otherDeduction" :label-width="labelWidth">
                        <el-input placeholder="其他扣除" v-model.number="form.otherDeduction"></el-input>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="closeDialog">取 消</el-button>
                    <el-button type="primary" @click="addParam('form')">确 定</el-button>
                </div>
            </el-dialog>
        </div>
    </div>
</template>

<script>
export default {
    name: 'basetable',
    inject: ['reload'],
    data() {
        return {
            query: {
                did: '',
                name: '',
                pageNum: 1,
                pageSize: 5
            },
            form: {
                pid: '',
                basicSalary: 5500   ,
                fullAttention: 0.0,
                subsidy: 0.0,
                positionSalary: 0.0,
                overtimeSalary: 46.88,
                leaveDeduction: 31.25,
                lateDeduction: 46.88,
                otherDeduction: 250
            },
            basicParam:{},
            depts:[],
            labelWidth: '100px',
            dialogFormVisible: false,
            tableData: [],
            paramPositions: [],
            multipleSelection: [],
            delList: [],
            pageTotal: 0,

            idx: -1,
            id: -1,

            rules: {
                pid: [{ required: true, message: '请选择职位', trigger: 'blur' }],
                basicSalary: [
                    { required: true, message: '请填入基础工资', trigger: 'blur' },
                    { type: 'number', message: '必须为数字值' }
                ],
                fullAttention: [
                    { required: true, message: '请填入全勤补贴', trigger: 'blur' },
                    { type: 'number', message: '必须为数字值' }
                ],
                subsidy: [
                    { required: true, message: '请填入其他补贴', trigger: 'blur' },
                    { type: 'number', message: '必须为数字值' }
                ],
                positionSalary: [
                    { required: true, message: '请填入职位补贴', trigger: 'blur' },
                    { type: 'number', message: '必须为数字值' }
                ],
                overtimeSalary: [
                    { required: true, message: '请填入加班补贴', trigger: 'blur' },
                    { type: 'number', message: '必须为数字值' }
                ],
                leaveDeduction: [
                    { required: true, message: '请填入请假扣除', trigger: 'blur' },
                    { type: 'number', message: '必须为数字值' }
                ],
                lateDeduction: [
                    { required: true, message: '请填入迟到,早退扣除', trigger: 'blur' },
                    { type: 'number', message: '必须为数字值' }
                ],
                otherDeduction: [
                    { required: true, message: '其他扣除', trigger: 'blur' },
                    { type: 'number', message: '必须为数字值' }
                ]
            }
        };
    },
    created() {
        this.getBasicParam();
        this.getAllPosition();
        this.getDepts();
        this.getParamPosition();
    },
    methods: {
        setParam(val) {
            
            this.form.overtimeSalary = parseFloat((val / 22 / 8) * 1.5).toFixed(2) - 0;
            this.form.leaveDeduction = parseFloat(val / 22 / 8).toFixed(2) - 0;
            this.form.lateDeduction = parseFloat((val / 22 / 8) * 1.5).toFixed(2) - 0;
            this.form.otherDeduction = parseFloat(val / 22).toFixed(2) - 0;
        },
        //获得部门
        async getDepts(){
            var d = await this.$http.get("/depts");
            if(d.code==200){
                this.depts=d.data
            }
        },

        // 获取 easy-mock 的模拟数据
        async init() {
            var d = await this.$http.post('/params', this.query);
            if (d.code == 200) {
                if (d.data != null) {
                    this.tableData = d.data.list;
                    this.pageTotal = d.data.total;
                } else {
                    this.tableData = [];
                }
            } else {
                this.$message.error(d.message);
            }
        },

        //获得基础参数
        async getBasicParam(){
            var d =await this.$http.get("/param/1")
            if(d.code==200){
              
                this.basicParam=d.data;
                this.setBasicValue(this.basicParam);
            }
        },
    
        //获取还没有工资参数的职位
        async getParamPosition() {
            var d = await this.$http.get('/paramPosition');
            if (d.code == 200) {
                this.paramPositions = d.data;
            }
        },

        //获取所有职位
        async getAllPosition() {
            var d = await this.$http.get('/positions');
            if (d.code == 200) {
                this.positions = d.data;
                this.init();
            }
        },

        setBasicValue(param){
            this.form.basicSalary=param.basicSalary;
            this.form.fullAttention=param.fullAttention;
            this.form.subsidy=param.subsidy;
            this.form.positionSalary=param.positionSalary;
            this.form.overtimeSalary=param.overtimeSalary;
            this.form.leaveDeduction=param.leaveDeduction;
            this.form.lateDeduction=param.lateDeduction;
            this.form.otherDeduction=param.otherDeduction;
        },

        formatterPName(row, column, cellValue, index) {
            for (var i = 0; i < this.positions.length; i++) {
                if(cellValue=='1'){
                    return "基础参数"
                }
                if (this.positions[i].id == cellValue) {
                    return this.positions[i].name;
                }
            }
        },

        // 触发搜索按钮
        handleSearch() {
            this.$set(this.query, 'pageIndex', 1);
            this.init();
        },
        // 编辑操作
        view(id) {
            this.$router.push({ path: '/getParam', query: { id: id } });
        },
        // 编辑操作
        edit(id) {
            this.$router.push({ path: '/updateParam', query: { id: id } });
        },
        // 删除操作
        del(id) {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(async () => {
                    var d = await this.$http.delete('/param/' + id);
                    if (d.code == 200) {
                        this.$message.success(d.message);
                        this.reload();
                    } else {
                        this.$message.error(d.message);
                    }
                })
                .catch(() => {});
        },
        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        //批量删除
        async delAllSelection() {
            const length = this.multipleSelection.length;
            var delName = '';
            for (let i = 0; i < length; i++) {
                this.delList.push(this.multipleSelection[i].id);
                delName = delName + this.multipleSelection[i].name + ' ';
            }
            this.$confirm('确定要删除 ' + delName + '吗？', '提示', {
                type: 'warning'
            })
                .then(async () => {
                    var d = await this.$http.post('/delparams', this.delList);
                    if (d.code == 200) {
                        this.$message.success(d.message);
                        this.multipleSelection = [];
                        this.reload();
                    } else {
                        this.$message.error(d.message);
                    }
                })
                .catch(() => {});
        },
        // 分页导航
        handlePageChange(val) {
            this.query.pageNum = val;
            this.init();
        },
        //关闭参数框
        closeDialog() {
            this.setBasicValue(this.basicParam);
            this.dialogFormVisible = false;
        },

        //添加参数
        addParam(formname) {
            this.$refs[formname].validate(async (valid) => {
                if (valid) {
                    var d = await this.$http.post('/param', this.form);
                    if (d.code == 200) {
                        this.$message.success(d.message);
                        this.reload();
                    } else {
                        this.$message.error(d.message);
                    }
                } else {
                
                    return false;
                }
            });
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
